<?php
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use app\admin\model\CategoryTypeModel;
use app\admin\model\CategoryModel;

class CategoryTypeController extends AdminBaseController
{
    
    /**
     * 标签分类
     * @adminMenu(
     *     'name'   => '标签分类',
     *     'parent' => 'admin/Category/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '标签分类',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $list = CategoryTypeModel::paginate(20);
        $this->assign('list',$list);
        
        return $this->fetch();
    }
    
    /**
     * 分类添加
     * @adminMenu(
     *     'name'   => '分类添加',
     *     'parent' => 'admin/CategoryType/index',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '分类添加',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        if(request()->isPost()){
            $request = $this->request->param();
            
            $categoryType = new CategoryTypeModel();
            $categoryType->name = $request['name'];
            $categoryType->label = $request['label'];
            $categoryType->remark = $request['remark'];
            $re = $categoryType->save();
            if($re){
                $this->success('添加成功');
            }
            else{
                $this->error('添加失败');
            }
        }
        
        return $this->fetch();
    }
    
    /**
     * 分类修改
     * @adminMenu(
     *     'name'   => '分类修改',
     *     'parent' => 'admin/CategoryType/index',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '分类修改',
     *     'param'  => ''
     * )
     */
    public function edit($id = null)
    {
        if(request()->isPost()){
            $request = $this->request->param();
            $categoryType = CategoryTypeModel::get($id);
            $categoryType->name = $request['name'];
            $categoryType->label = $request['label'];
            $categoryType->remark = $request['remark'];
            $re = $categoryType->save();
            if($re){
                $this->success('修改成功');
            }
            else{
                $this->error('修改失败');
            }
        }
        else{
            $data = CategoryTypeModel::get($id);
            $this->assign('data',$data);
            
            return $this->fetch();
        }
    }
    
    /**
     * 分类删除
     * @adminMenu(
     *     'name'   => '分类删除',
     *     'parent' => 'admin/CategoryType/index',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '分类删除',
     *     'param'  => ''
     * )
     */
    public function delete($id = null)
    {
        if(empty($id)){
            $this->error('参数错误');
        }
        $result = CategoryModel::destroy(['type_id'=>$id]);
        $result = CategoryTypeModel::destroy($id);
        if ($result) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}